package com.cskaoyan.mall.order.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cskaoyan.mall.common.model.BaseEntity;
import com.cskaoyan.mall.order.constant.OrderType;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Data
@TableName("order_info")
public class OrderInfo extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @TableField("parent_order_id")
    private Long parentOrderId; //父订单编号，暂时没用

    @TableField("order_status")
    private String orderStatus; //订单状态

    @TableField("user_id")
    private Long userId;  //用户id，userId

    @TableField("payment_way")
    private String paymentWay = "ONLINE";  //默认 线上

    @TableField("consignee")
    private String consignee;  //收货人姓名

    @TableField("consignee_tel")
    private String consigneeTel; //收货人电话

    @TableField("delivery_address")
    private String deliveryAddress; //发货地址

    @TableField("total_amount")
    private BigDecimal totalAmount;  //订单中商品总价格

    @TableField("original_total_amount")
    private BigDecimal originalTotalAmount; //订单中商品原始总价格

    @TableField("order_comment")
    private String orderComment;  //订单评论 忽略

    @TableField("out_trade_no")
    private String outTradeNo;  //使用雪花算法生产唯一id

    @TableField("trade_body")
    private String tradeBody; //订单描述：使用商品名称

    @TableField("order_type")
    private String orderType = OrderType.NORMAL_ORDER.name(); //订单类型：普通订单|秒杀订单

    @TableField("tracking_no")
    private String trackingNo;

    // 可退款日期
    @TableField("refundable_time")
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    private Date refundableTime;

    @TableField("expire_time")
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    private Date expireTime;    //订单过期时间

    @TableField(exist = false)
    private List<OrderDetail> orderDetailList;  //订单中商品详细信息

    @TableField(exist = false)
    private String wareId;

    // 计算总价格
    public void sumTotalAmount(){
        BigDecimal totalAmount = new BigDecimal("0");
        //  计算最后
        for (OrderDetail orderDetail : orderDetailList) {
            BigDecimal skuTotalAmount = orderDetail.getOrderPrice().multiply(new BigDecimal(orderDetail.getSkuNum()));
            totalAmount = totalAmount.add(skuTotalAmount);
        }
        this.setTotalAmount(totalAmount);
        this.setOriginalTotalAmount(totalAmount);
    }
}
